package br.edu.ufcg.ccc.wordcount;

import java.io.IOException;
import java.util.StringTokenizer;

import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapred.MapReduceBase;
import org.apache.hadoop.mapred.Mapper;
import org.apache.hadoop.mapred.OutputCollector;
import org.apache.hadoop.mapred.Reporter;

import br.edu.ufcg.ccc.util.TwitterParseUtil;

/**
 * 
 * @author victorclf
 * @author pedroyossis
 */
@SuppressWarnings("deprecation")
public class WordCountMap extends MapReduceBase implements
		Mapper<LongWritable, Text, Text, IntWritable> {
	private final static IntWritable one = new IntWritable(1);
	private Text word = new Text();

	public void map(LongWritable key, Text value,
			OutputCollector<Text, IntWritable> output, Reporter reporter)
			throws IOException {
		String line = value.toString();
		line = TwitterParseUtil.preformatLine(line);
		
		StringTokenizer tokenizer = new StringTokenizer(line);
		while (tokenizer.hasMoreTokens()) {
			String token = tokenizer.nextToken();
			token = TwitterParseUtil.preformatToken(token);
			if (TwitterParseUtil.isValidWord(token) && !TwitterParseUtil.isTrivialWord(token)) {
				word.set(token);
				output.collect(word, one);
			}
		}
	}
}